package net.zoneland.knowledge.enums;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public enum CompanyEnum implements Supplier<CompanyEnum> {
    /**
     * 省公司直属党委.
     */
    PROVINCE_PARTY("C58B5479-73BF-4876-BF07-50513AA482CD", "省公司直属党委", 100, "省公司直属党委"),
    /**
     * 杭州分公司党委.
     */
    HZ_PARTY("A84B9E94-CD82-453F-87A0-EF7FA2112813", "杭州分公司党委", 110,"杭州分公司"),
    /**
     * 杭州分公司党委.
     */
    NB_PARTY("DD4E5013-68C1-4874-8562-6A0C7F52F883", "宁波分公司党委", 120,"宁波分公司"),
    /**
     * 温州分公司党委.
     */
    WZ_PARTY("0F0D069B-8575-427E-80AF-36D062DF2280", "温州分公司党委", 130,"温州分公司"),
    /**
     * 金华分公司党委.
     */
    JH_PARTY("658569F8-381E-4639-97E4-49D746B3D411", "金华分公司党委", 140, "金华分公司"),
    /**
     * 台州分公司党委.
     */
    TZ_PARTY("26DF6207-80DB-4C2F-B4D8-4ECECFEDAD71", "台州分公司党委", 150, "台州分公司"),
    /**
     * 绍兴分公司党委.
     */
    SX_PARTY("7AFF9BBB-59F2-469E-B7DB-4BDF35F258FB", "绍兴分公司党委", 160, "绍兴分公司"),
    /**
     * 嘉兴分公司党委.
     */
    JX_PARTY("54D08A88-2704-4D1A-B49A-A50BB0814D2E", "嘉兴分公司党委", 170, "嘉兴分公司"),
    /**
     * 湖州分公司党委.
     */
    HU_PARTY("B50D1047-D742-48C8-BBB8-D88AB8580A48", "湖州分公司党委", 180,"湖州分公司"),
    /**
     * 丽水分公司党委.
     */
    LS_PARTY("3143E88E-67CD-4D22-BF99-813FDCAD9404", "丽水分公司党委", 190, "丽水分公司"),
    /**
     * 舟山分公司党委.
     */
    ZS_PARTY("C227B30D-AB8C-4FE1-A496-4575D4F016CA", "舟山分公司党委", 200,"舟山分公司"),
    /**
     * 衢州分公司党委.
     */
    QZ_PARTY("B7B7FC5F-9EF0-4340-83C8-381FB536516C", "衢州分公司党委", 210,"衢州分公司"),
    /**
     * 其它.
     */
    OTHER("B7B7FC5F-9EF0-4340-83C8-381FB536516C", "其它党委", 211,"其它公司");


    /**
     * 公司dn.
     */
    private String orgUuid;

    private String name;

    /**
     * 顺序.
     */
    private Integer order;

    private String aliasName;

    CompanyEnum(final String orgUuid, final String name, final Integer order, final String aliasName) {
        this.name = name;
        this.orgUuid = orgUuid;
        this.aliasName = aliasName;
        this.order = order;
    }

    /**
     * 根据公司名称获取公司.
     * @param companyName
     * @return
     */
    public static CompanyEnum getEnumByCompanyName(final String companyName){
        return  Arrays.stream(CompanyEnum.values())
                .filter(company -> StringUtils.equals(company.aliasName, companyName))
                .findAny()
                .orElseGet(CompanyEnum.PROVINCE_PARTY);
    }

    /**
     * 根据名称猎取该对象.
     * @param name 公司名称
     * @return CompanyEnum
     */
    public static CompanyEnum getEnumByName(String name) {
        return  Arrays.stream(CompanyEnum.values()).filter(use -> StringUtils.equals(use.name, name)).findAny().orElseGet(CompanyEnum.OTHER);
    }

    private static List<CompanyEnum> companyEnums = Lists.newArrayList(
            PROVINCE_PARTY,
            HZ_PARTY,
            NB_PARTY,
            WZ_PARTY,
            JH_PARTY,
            TZ_PARTY,
            SX_PARTY,
            JX_PARTY,
            HU_PARTY,
            LS_PARTY,
            ZS_PARTY,
            QZ_PARTY

    );

    public static List<CompanyEnum> getCompanyEnums() {
        return Collections.unmodifiableList(companyEnums);
    }

    /**
     * 列表.
     * @return List<String>  String公司名称
     */
    public static List<String> getEnumValueList() {
        return  Stream.of(CompanyEnum.values())
                .filter(Predicate.isEqual(CompanyEnum.OTHER).negate())
                .map(CompanyEnum::getAliasName)
                .collect(Collectors.toList());
    }

    /**
     * Gets the value of name. *
     *
     * @return the value of name
     */
    public String getName() {
        return name;
    }

    public String getOrgUuid() {
        return orgUuid;
    }

    public Integer getOrder() {
        return order;
    }

    public String getAliasName() {
        return aliasName;
    }

    /**
     * Gets a result.
     *
     * @return a result
     */
    @Override
    public CompanyEnum get() {
        return CompanyEnum.OTHER;
    }

    public static int  comparator(final String companyName1, final String companyName2) {
        final CompanyEnum key1 = CompanyEnum.getEnumByCompanyName(companyName1);
        final CompanyEnum key2 = CompanyEnum.getEnumByCompanyName( companyName2);
        return key2.getOrder() - key1.getOrder();
    }
}
